﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Jage.Logic.Resources;

namespace Jage.GameEditor.Controls.Spritesheets
{
    public partial class SpritesheetPreview : UserControl
    {
        public SpritesheetPreview()
        {
            InitializeComponent();
        }

        public void SetSpritesheet(Data.Spritesheet spritesheet)
        {
            Spritesheet = spritesheet;
            if (Spritesheet != null && SpritesheetBitmap != null)
            {
                labelSize.Text = string.Format("Size: {0}x{1} px    Contains: {2} sprites", SpritesheetBitmap.Width, SpritesheetBitmap.Height, Spritesheet.Sprites.Count);
            }
            else
            {
                labelSize.Text = string.Empty;
            }
        }

        private void PanelPreviewSpritePaint(object sender, PaintEventArgs e)
        {
            if (Spritesheet == null || SpritesheetBitmap == null || String.IsNullOrWhiteSpace(Spritesheet.Resource)) return;
            var spriteWidth = SpritesheetBitmap.Width;
            var width = Math.Min(Width, spriteWidth);
            var zoom = (float) width/spriteWidth;
            e.Graphics.DrawImage(SpritesheetBitmap, new Rectangle(0, 0, width, (int)(zoom * SpritesheetBitmap.Height)));
            foreach (var sprite in Spritesheet.Sprites)
            {
                var sprite1 = sprite;
                if (SelectedSpriteIds != null)
                {
                    if (SelectedSpriteIds.Contains(sprite.SpriteId))
                        e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 0, 0, 255)),
                                                 new Rectangle((int) (sprite.X*zoom), (int) (sprite.Y*zoom),
                                                               (int) (sprite.Width*zoom), (int) (sprite.Height*zoom)));
                }
                e.Graphics.DrawRectangle(Pens.Blue, new Rectangle((int)(sprite.X * zoom), (int)(sprite.Y * zoom), (int)(sprite.Width * zoom), (int)(sprite.Height * zoom)));
                DrawOriginMark(e.Graphics, new Point((int)((sprite.X + sprite.OriginX) * zoom), (int)((sprite.Y + sprite.OriginY) * zoom)));
            }
        }

        private void DrawOriginMark(Graphics graphics, Point origin)
        {
            graphics.DrawLine(Pens.Cyan, origin.X, origin.Y - 2, origin.X, origin.Y + 2);
            graphics.DrawLine(Pens.Cyan, origin.X - 2, origin.Y, origin.X + 2, origin.Y);
        }

        private Bitmap _spritesheetBitmap;
        private Bitmap SpritesheetBitmap
        {
            get
            {
                if (_spritesheetBitmap == null && Spritesheet != null) _spritesheetBitmap = ResourceIO.GetBitmap(Spritesheet.Resource);
                return _spritesheetBitmap;
            }
        }

        private Data.Spritesheet _spritesheet;
        private Data.Spritesheet Spritesheet
        {
            get
            {
                return _spritesheet;
            }
            set
            {
                _spritesheet = value;
                if (_spritesheetBitmap != null) _spritesheetBitmap.Dispose();
                _spritesheetBitmap = null;
                Visible = _spritesheet != null;
                Refresh();
            }
        }

        public List<Guid> SelectedSpriteIds { get; set; }

    }
}
